package com.acoreful.acf2admin.web.controller;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.acoreful.acf2admin.commons.msg.MsgResult;
import com.acoreful.acf2admin.commons.msg.ResultEnums;
import com.google.common.collect.Lists;

/**
 * 文档上传
 * @author acoresoft
 *
 */
@Controller
@RequestMapping("/uploadFile")
public class UploadFileController {

	/**
	 * 
	 * @param files	文件
	 * @param datas	参数
	 * @param req
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	@ResponseBody
	public MsgResult uploadFile(@RequestParam("file") MultipartFile[] files,HttpServletRequest req){
		String upload_dir=req.getSession().getServletContext().getRealPath("/")+File.separator+"uploadTemp"+File.separator;
		List<File> targetFiles=Lists.newArrayList();
		File file=null;
		try {
			FileUtils.forceMkdir(new File(upload_dir));//强制创建目录
			for (MultipartFile cfile : files) {
				file=new File(upload_dir+URLEncoder.encode(cfile.getOriginalFilename(),"UTF-8"));
				cfile.transferTo(file);
				targetFiles.add(file);
			}
		} catch (IllegalStateException | IOException  e) {
			e.printStackTrace();
			return MsgResult.getMsgResult("001","上传失败");
		}
		return MsgResult.getMsgResult(ResultEnums.SUCCESS);
	}
}
